package server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class affair {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement p1 =null;
        PreparedStatement p2 =null;
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybattis?serverTimezone=UTC","root","root");
            //手动开启事务
            conn.setAutoCommit(false);

            String sql1 = "update account set  money = (money - 100) where id = ? ";
            p1 = conn.prepareStatement(sql1);
            p1.setInt(1,1);

            String sql2 = "update account set  money =(money + 100) where id = ? ";
            p2 = conn.prepareStatement(sql2);
            p2.setInt(1,2);

            int i = p1.executeUpdate();

            //提交
            conn.commit();


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("sql语句异常");
        }finally {
            try {
                conn.close();
                p1.close();
                p2.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
